package com.dcms.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
 * ClassName: StaticizingUtil
 * 所属功能模块:
 * 功能描述:
 *
 * @author zhouyijin
 * @Date 2018年02月19日 18:45
 * @since Ver 1.0
 */
public class StaticizingUtil {

    private static final Logger LOGGER = Logger.getLogger(StaticizingUtil.class);

    /**
     * 将给定的url 进行一次http访问,拿到的结果当做字符串返回
     * @param url
     * @return
     */
    public static String getHtml(String url){
        HttpClient client = new HttpClient();
        GetMethod getMethod = new GetMethod(url);

        String response = null;

        try {
            client.executeMethod(getMethod);//使用httpclient访问
            int statusCode = getMethod.getStatusCode();
            if (statusCode>=400){
                LOGGER.error("网页静态化错误: ("+url+")该网页访问错误!");
                throw new RuntimeException("网页静态化错误: ("+url+")该网页访问错误!");
            }
            response = getMethod.getResponseBodyAsString();
        } catch (IOException e) {
            LOGGER.error("网页静态化错误: ("+url+")该网页无法访问!");
            throw new RuntimeException("网页静态化错误: ("+url+")该网页无法访问!");
        }

        return response;
    }


}
